package hsh.machine.components;

import hsh.machine.instructions.Instruction;

public class Processor {
	private int pcRegister;
	private int pcInterruptionRegister;
	private String causeRegister;
	private Bus bus;
	
	
	public Processor(Bus bus) {
		super();
		this.bus = bus;
	}

	public void execute(int pc,int timeInInstructions){
		Instruction current = null;
		pcRegister = pc;
		try{
			current = bus.readPrimaryMemory(pcRegister);
		}catch(Exception e){
			causeRegister = "OUT_OF_MEMORY";
			pcInterruptionRegister = pcRegister;
		}
		
		
		if(current != null){
			if(current.getOpcode().equalsIgnoreCase("SYSCALL")){
				causeRegister = "SYSCALL";
				pcInterruptionRegister = pcRegister;
				return;
			}
		}
		
		pcRegister++;
		
		if(timeInInstructions > 0){
			execute(pcRegister,timeInInstructions-1);
		}else if(timeInInstructions == 0){
			causeRegister = "TIMER";
			pcInterruptionRegister = pcRegister;
			return;
		}
	}

	public int getPcInterruptionRegister() {
		return pcInterruptionRegister;
	}

	public void setPcInterruptionRegister(int pcInterruptionRegister) {
		this.pcInterruptionRegister = pcInterruptionRegister;
	}

	public String getCause() {
		return causeRegister;
	}

	public void setCause(String cause) {
		this.causeRegister = cause;
	}

	public int getPcRegister() {
		return pcRegister;
	}

	
	public String getCauseRegister() {
		return causeRegister;
	}

	
	
}
